package com.my.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.my.annotation.Param;
import com.my.annotation.Return;
import com.my.demo.model.Book;
import com.my.demo.model.BookSelector;
import com.my.demo.service.BookService;
import com.my.framework.dao.Pagination;
import com.my.framework.web.annotation.Query;
import com.my.framework.web.annotation.Update;
import com.my.framework.web.mvc.controller.BaseController;
import com.my.model.Request;
import com.my.model.Response;

@Controller
@RequestMapping("/book/api")
public class BookApiController extends BaseController {

	@Autowired
	private BookService bookService;

	@Update(name = "新增图书", path = "/insert")
	@Param("要新增的图书")
	@Return("新增的图书信息")
	public Response<Book> insert(@RequestBody Request<Book> bookInfo) {
		Book book = bookInfo.getContent();
		prepareAdd(book);
		Book bk = bookService.insert(book);
		return Response.success(bk);
	}

	@Query(name = "查询图书", path = "/select")
	@Param("查询条件")
	@Return("查询结果")
	public Response<Pagination> select(@RequestBody Request<BookSelector> selector) {
		BookSelector select = selector.getContent();
		Book book = new Book();
		book.setBookName(select.getBookName());
		Pagination page = bookService.selectPage(book, select.getOffset(), select.getLimit());
		return Response.success(page);
	}

}
